<?php

namespace app\index\controller;

use Alipay\Alipayobj;
use app\api\controller\Cards;
use app\api\controller\CardsThree;
use app\api\controller\CreateNotify;
use app\api\controller\DepositPay;
use app\api\controller\Huifupay;
use app\api\controller\Lftpay;
use app\api\controller\MachThree;
use app\api\controller\NotCardPay;
use app\api\controller\OfficialPay;
use app\api\controller\Order;
use app\api\controller\Pay;
use app\api\controller\Pub;
use app\api\controller\WxAppCreateOrder;
use app\api\controller\WxCardus;
use app\api\job\SendNotify;
use app\common\controller\Bonus;
use app\common\controller\ChildCardModify;
use app\common\controller\Deviceobj;
use app\common\controller\TemplateMsg;
use app\common\model\Agent;
use app\common\model\CardCzgive;
use app\common\model\CardGeneral;
use app\common\model\CardMember;
use app\common\model\CardRank;
use app\common\model\CardUserGet;
use app\common\model\Deposit;
use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\WxOpen;
use Endroid\QrCode\QrCode;
use think\Config;
use think\Db;
use think\helper\hash\Md5;
use Wechat\Wxobjopen;
use Weixinpay\Wxtk;

class Index
{

    public function test_tz(){
        $bus_orderid = "bus1355-20201103110238994463";
        $CreateNotice = new CreateNotify();
        $res = $CreateNotice->balance_rec_notify($bus_orderid);
        var_dump(json_encode($res,256));
    }

    //正式的试试
    public function test_cardusadd(){

//        var_dump(123123);
//        exit();
        $arr = '{"get_limit":1000,"brand_name":"地方","card_type":"MEMBER_CARD","logo_url":"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/VtwMJoicTCCXJfkwqH0lia3Pst5CgJ3iap4OUxxHIIm6718j7G1sBwnS3JBAia3B0vfeibc1f1e6SwcwYgibXNKrHt2w\/0","logo_url_abs":"http:\/\/property.wunengkeji.com\/uploads\/img\/20200418\/c0582e486351fd7c6f8ae4adcb67a565.jpg","title":"风格","pic_url":"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/VtwMJoicTCCXJfkwqH0lia3Pst5CgJ3iap4ezkl1H5nian2ZGI7M5gDwFplribxzhb0n599ibiaNHdXYESk4HicFqRvbwQ\/0","pic_url_abs":"http:\/\/property.wunengkeji.com\/uploads\/img\/20200418\/5d6f567771d90e034898e7741c10eb0c.jpg","color":"#63b359","base_info":"{\"color\":\"Color010\",\"get_limit\":1000,\"brand_name\":\"地方\",\"title\":\"风格\",\"notice\":\"风格\",\"logo_url\":\"http:\\\/\\\/mmbiz.qpic.cn\\\/mmbiz_jpg\\\/VtwMJoicTCCXJfkwqH0lia3Pst5CgJ3iap4OUxxHIIm6718j7G1sBwnS3JBAia3B0vfeibc1f1e6SwcwYgibXNKrHt2w\\\/0\",\"code_type\":\"CODE_TYPE_QRCODE\",\"service_phone\":\"15896325632\",\"description\":\"发过火\",\"date_info\":{\"type\":\"DATE_TYPE_PERMANENT\"},\"sku\":{\"quantity\":1000000},\"use_custom_code\":true,\"can_share\":false,\"can_give_friend\":false,\"need_push_on_view\":false,\"center_title\":\"\",\"center_sub_title\":\"\",\"center_url\":\"\",\"use_all_locations\":true,\"custom_url_name\":\"\",\"custom_url_sub_title\":\"\",\"custom_url\":\"\",\"promotion_url_name\":\"\",\"promotion_url\":\"\",\"promotion_url_sub_title\":\"\"}","supply_bonus":"2","is_dedu_cards":1,"supply_balance":"","center_type":"2","is_center":"2","cover_type":"1","is_cell1":"1","is_cell2":"1","is_cell3":"1","is_cell4":"1","is_money":1,"is_field2":1,"is_field3":"1","is_locations":"","center_pay":"","center":"","is_bonus":2,"bonus_rule":"","custom_field2":"{\"name\":\"余额\",\"url\":\"http:\\\/\\\/property.wunengkeji.com\\\/mobiles.php\\\/#\\\/balance\\\/balancelist\\\/sysid\\\/1\"}","custom_field3":{"name":"","sub_name":"","url":"","title":"","app_brand_user_name":"","app_brand_pass":""},"custom_cell1":{"name":"","tips":"","url":"","title":"","app_brand_user_name":"","app_brand_pass":""},"custom_cell2":{"name":"","tips":"","url":"","title":"","app_brand_user_name":"","app_brand_pass":""},"custom_cell3":{"name":"","tips":"","url":"","title":"","app_brand_user_name":"","app_brand_pass":""},"custom_cell4":{"name":"","tips":"","url":"","title":"","app_brand_user_name":"","app_brand_pass":""},"discount":"1","prerogative":"从v不过","statu":0,"stocks":1000000,"activate":2,"fire":0,"create_time":1587181274,"sysid":"1","param_info":"{\"get_limit\":1000,\"brand_name\":\"地方\",\"card_type\":\"MEMBER_CARD\",\"logo_url\":\"http:\\\/\\\/mmbiz.qpic.cn\\\/mmbiz_jpg\\\/VtwMJoicTCCXJfkwqH0lia3Pst5CgJ3iap4OUxxHIIm6718j7G1sBwnS3JBAia3B0vfeibc1f1e6SwcwYgibXNKrHt2w\\\/0\",\"logo_url_abs\":\"http:\\\/\\\/property.wunengkeji.com\\\/uploads\\\/img\\\/20200418\\\/c0582e486351fd7c6f8ae4adcb67a565.jpg\",\"title\":\"风格\",\"pic_url\":\"http:\\\/\\\/mmbiz.qpic.cn\\\/mmbiz_jpg\\\/VtwMJoicTCCXJfkwqH0lia3Pst5CgJ3iap4ezkl1H5nian2ZGI7M5gDwFplribxzhb0n599ibiaNHdXYESk4HicFqRvbwQ\\\/0\",\"pic_url_abs\":\"http:\\\/\\\/property.wunengkeji.com\\\/uploads\\\/img\\\/20200418\\\/5d6f567771d90e034898e7741c10eb0c.jpg\",\"color\":\"#63b359\",\"base_info\":\"{\\\"color\\\":\\\"Color010\\\",\\\"get_limit\\\":1000,\\\"brand_name\\\":\\\"地方\\\",\\\"title\\\":\\\"风格\\\",\\\"notice\\\":\\\"风格\\\",\\\"logo_url\\\":\\\"http:\\\\\\\/\\\\\\\/mmbiz.qpic.cn\\\\\\\/mmbiz_jpg\\\\\\\/VtwMJoicTCCXJfkwqH0lia3Pst5CgJ3iap4OUxxHIIm6718j7G1sBwnS3JBAia3B0vfeibc1f1e6SwcwYgibXNKrHt2w\\\\\\\/0\\\",\\\"code_type\\\":\\\"CODE_TYPE_QRCODE\\\",\\\"service_phone\\\":\\\"15896325632\\\",\\\"description\\\":\\\"发过火\\\",\\\"date_info\\\":{\\\"type\\\":\\\"DATE_TYPE_PERMANENT\\\"},\\\"sku\\\":{\\\"quantity\\\":1000000},\\\"use_custom_code\\\":true,\\\"can_share\\\":false,\\\"can_give_friend\\\":false,\\\"need_push_on_view\\\":false,\\\"center_title\\\":\\\"\\\",\\\"center_sub_title\\\":\\\"\\\",\\\"center_url\\\":\\\"\\\",\\\"use_all_locations\\\":true,\\\"custom_url_name\\\":\\\"\\\",\\\"custom_url_sub_title\\\":\\\"\\\",\\\"custom_url\\\":\\\"\\\",\\\"promotion_url_name\\\":\\\"\\\",\\\"promotion_url\\\":\\\"\\\",\\\"promotion_url_sub_title\\\":\\\"\\\"}\",\"supply_bonus\":\"2\",\"is_dedu_cards\":1,\"supply_balance\":\"\",\"center_type\":\"2\",\"is_center\":\"2\",\"cover_type\":\"1\",\"is_cell1\":\"1\",\"is_cell2\":\"1\",\"is_cell3\":\"1\",\"is_cell4\":\"1\",\"is_money\":1,\"is_field2\":1,\"is_field3\":\"1\",\"is_locations\":\"\",\"center_pay\":\"\",\"center\":\"\",\"is_bonus\":2,\"bonus_rule\":\"\",\"custom_field2\":\"{\\\"name\\\":\\\"余额\\\",\\\"url\\\":\\\"http:\\\\\\\/\\\\\\\/property.wunengkeji.com\\\\\\\/mobiles.php\\\\\\\/#\\\\\\\/balance\\\\\\\/balancelist\\\\\\\/sysid\\\\\\\/1\\\"}\",\"custom_field3\":{\"name\":\"\",\"sub_name\":\"\",\"url\":\"\",\"title\":\"\",\"app_brand_user_name\":\"\",\"app_brand_pass\":\"\"},\"custom_cell1\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\",\"title\":\"\",\"app_brand_user_name\":\"\",\"app_brand_pass\":\"\"},\"custom_cell2\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\",\"title\":\"\",\"app_brand_user_name\":\"\",\"app_brand_pass\":\"\"},\"custom_cell3\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\",\"title\":\"\",\"app_brand_user_name\":\"\",\"app_brand_pass\":\"\"},\"custom_cell4\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\",\"title\":\"\",\"app_brand_user_name\":\"\",\"app_brand_pass\":\"\"},\"discount\":\"1\",\"prerogative\":\"从v不过\",\"statu\":0,\"stocks\":1000000,\"activate\":2,\"fire\":0,\"create_time\":1587181274,\"sysid\":\"1\"}","param_detail":"{\"method\":\"cardusadd\",\"rid\":\"4\",\"cover_type\":\"1\",\"is_discount\":\"1\",\"supply_bonus\":\"2\",\"discount\":\"1\",\"is_field3\":\"1\",\"prerogative\":\"从v不过\",\"background_pic_url\":\"http:\\\/\\\/mmbiz.qpic.cn\\\/mmbiz_jpg\\\/VtwMJoicTCCXJfkwqH0lia3Pst5CgJ3iap4ezkl1H5nian2ZGI7M5gDwFplribxzhb0n599ibiaNHdXYESk4HicFqRvbwQ\\\/0\",\"background_pic_url_abs\":\"http:\\\/\\\/property.wunengkeji.com\\\/uploads\\\/img\\\/20200418\\\/5d6f567771d90e034898e7741c10eb0c.jpg\",\"bonus\":{\"init_increase_bonus\":\"1\",\"cost_money_unit\":\"1\",\"increase_bonus\":\"1\"},\"custom_field3\":{\"name\":\"\",\"sub_name\":\"\",\"url\":\"\",\"title\":\"\",\"app_brand_user_name\":\"\",\"app_brand_pass\":\"\"},\"base\":{\"color\":\"#63b359\",\"brand_name\":\"地方\",\"title\":\"风格\",\"logo_url\":\"http:\\\/\\\/mmbiz.qpic.cn\\\/mmbiz_jpg\\\/VtwMJoicTCCXJfkwqH0lia3Pst5CgJ3iap4OUxxHIIm6718j7G1sBwnS3JBAia3B0vfeibc1f1e6SwcwYgibXNKrHt2w\\\/0\",\"logo_url_abs\":\"http:\\\/\\\/property.wunengkeji.com\\\/uploads\\\/img\\\/20200418\\\/c0582e486351fd7c6f8ae4adcb67a565.jpg\",\"code_type\":\"CODE_TYPE_QRCODE\",\"is_center\":\"2\",\"center\":\"微信支付\",\"center_title\":\"微信支付\",\"center_sub_title\":\"\",\"center_url\":\"\",\"center_app_brand_user_name\":\"\",\"center_app_brand_pass\":\"\",\"description\":\"发过火\",\"service_phone\":\"15896325632\",\"notice\":\"风格\"},\"is_cell1\":\"1\",\"is_cell2\":\"1\",\"is_cell3\":\"1\",\"is_cell4\":\"1\",\"custom_cell1\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\",\"title\":\"\",\"app_brand_user_name\":\"\",\"app_brand_pass\":\"\"},\"custom_cell2\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\",\"title\":\"\",\"app_brand_user_name\":\"\",\"app_brand_pass\":\"\"},\"custom_cell3\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\",\"title\":\"\",\"app_brand_user_name\":\"\",\"app_brand_pass\":\"\"},\"custom_cell4\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\",\"title\":\"\",\"app_brand_user_name\":\"\",\"app_brand_pass\":\"\"},\"token\":\"IkSjyKaM6NL6PHt6Uk0HAyawzMbuFLVpB3CRingUnP\",\"custom_field2\":\"{\\\"name\\\":\\\"余额\\\",\\\"url\\\":\\\"http:\\\\\\\/\\\\\\\/property.wunengkeji.com\\\\\\\/mobiles.php\\\\\\\/#\\\\\\\/balance\\\\\\\/balancelist\\\\\\\/sysid\\\\\\\/1\\\"}\",\"is_field2\":1}","card_info":"{\"card\":{\"card_type\":\"MEMBER_CARD\",\"member_card\":{\"background_pic_url\":\"http:\\\/\\\/mmbiz.qpic.cn\\\/mmbiz_jpg\\\/VtwMJoicTCCXJfkwqH0lia3Pst5CgJ3iap4ezkl1H5nian2ZGI7M5gDwFplribxzhb0n599ibiaNHdXYESk4HicFqRvbwQ\\\/0\",\"base_info\":{\"color\":\"Color010\",\"get_limit\":1000,\"brand_name\":\"地方\",\"title\":\"风格\",\"notice\":\"风格\",\"logo_url\":\"http:\\\/\\\/mmbiz.qpic.cn\\\/mmbiz_jpg\\\/VtwMJoicTCCXJfkwqH0lia3Pst5CgJ3iap4OUxxHIIm6718j7G1sBwnS3JBAia3B0vfeibc1f1e6SwcwYgibXNKrHt2w\\\/0\",\"code_type\":\"CODE_TYPE_QRCODE\",\"service_phone\":\"15896325632\",\"description\":\"发过火\",\"date_info\":{\"type\":\"DATE_TYPE_PERMANENT\"},\"sku\":{\"quantity\":1000000},\"use_custom_code\":true,\"can_share\":false,\"can_give_friend\":false,\"need_push_on_view\":false,\"center_title\":\"\",\"center_sub_title\":\"\",\"center_url\":\"\",\"use_all_locations\":true,\"custom_url_name\":\"\",\"custom_url_sub_title\":\"\",\"custom_url\":\"\",\"promotion_url_name\":\"\",\"promotion_url\":\"\",\"promotion_url_sub_title\":\"\"},\"prerogative\":\"从v不过\",\"supply_bonus\":false,\"supply_balance\":false,\"custom_field2\":{\"name\":\"余额\",\"url\":\"http:\\\/\\\/property.wunengkeji.com\\\/mobiles.php\\\/#\\\/balance\\\/balancelist\\\/sysid\\\/1\"},\"custom_field3\":{\"name\":\"\",\"url\":\"\"},\"custom_cell1\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\"},\"custom_cell2\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\"},\"auto_activate\":true,\"discount\":99}}}","method":"cardus_create","appid":"20180501","signaturenonce":26430042,"merchantid":"1328","timestamp":1587181274,"version":"2.0"}';
        $data = json_decode($arr,true);
        $WxCardus = new WxCardus();
        $res = $WxCardus->cardus_create($data);
        var_dump($res);
    }


    public function test_paycx(){
        $msg['out_trade_no'] = 'H1328-20191214153212882657';
        $Paylog = new Paylog();
        $map['orderid'] = $msg['out_trade_no'];
        $payinfo = $Paylog->where($map)->find();
        if ($payinfo){
            if ($payinfo['tzzt'] == '0') {
                $notify_param['out_trade_no'] = $msg['out_trade_no'];
                $notify_param['merchantid'] = $payinfo['mid'];
                $WxAppCreateOrder = new WxAppCreateOrder();
                $WxAppCreateOrder->wxorderquery($notify_param);
//                $this->sendnotify($notify_param);
            } elseif ($payinfo['tzzt'] == 1) {
                $res = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
                echo $res;
            }
        }

    }


    public function test_cardinfo(){
        $data['merchantid']  = 501;
        $data['code']  = 127359541321;
        $res = $this->cardqinfo($data);
        var_dump($res);

    }
    public function test21(){
        $Deposit=new Paylog();
        $list= $Deposit->where(['pay_cate'=>"wx_deposit"])->field('id,pay_cate,price,yf_price')->select();
        foreach($list as $k=>$v){
                $res=$Deposit->where(['id'=>$v['id']])->update(['yf_price'=>$v['price']]);
                var_dump($res);


        }
        //var_dump(collection($list)->toArray());
    }

    //获取核销状态    0可用/1不可用
    public function consumestatu($card_id, $code)
    {
        $data = [
            'card_id' => $card_id,
            'code' => $code,
            'check_consume' => true
        ];
        $options = $this->options($data['card_id']);
        $wxObj = new Wxobjopen($options);
        $res = $wxObj->getCardState($data);

        if ($res['errcode'] == 0 && $res['errmsg'] == 'ok') {
            return '0';
        } else {
            return '1';
        }

    }

    public function cardqinfo($params)
    {
//        try {
        //   return json($params);
        $Merchant = new Merchant();
        $top_mid = $Merchant->where('id', $params['merchantid'])->value('top_mid');
        $midlist = $Merchant->where('top_mid', $top_mid)->field('id')->select();
        foreach ($midlist as $k => $v) {
            $midarr[] = $v['id'];
        }

        $mid = implode(',', $midarr);
        $map['mid'] = ['in', $mid];
        $map['top_mid'] = $top_mid;
        $map['code'] = $params['code'];
        $map['card_type'] = ['neq', 'MEMBER_CARD'];
        $CardUserGet = new CardUserGet();
        $CardGeneral = new CardGeneral();
        //用户
        $userinfo = $CardUserGet->where($map)->field('card_id,code,create_time,have,statu')->find();

        if ($userinfo) {
            if ($userinfo['statu'] == 0) {
                if ($userinfo['have'] == 1) {
                    $mab['card_id'] = $userinfo['card_id'];
                    $cardinfo = $CardGeneral->where($mab)->field('title,brand_name,logo_url,card_type')->find();
                    $brand_name = $Merchant->where(['id' => $params['merchantid']])->value('merchant');
                    //卡券详情

                    $userinfo['title'] = $cardinfo['title'];
                    $userinfo['merchantname'] = $brand_name;
                    $userinfo['logo_url'] = $cardinfo['logo_url'];
                    $userinfo['card_type'] = $cardinfo['card_type'];
                    //返回核销状态    0可用/1不可用
                        $userinfo['consume_statu'] = $this->consumestatu($userinfo['card_id'], $userinfo['code']);
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['return_msg'] = 'ok';
                    $rarr['result_code'] = 'SUCCESS';
                    $rarr['result_msg'] = "获取成功";
                    $rarr['info'] = $userinfo;
                } elseif ($userinfo['have'] == 0) {
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['return_msg'] = 'ok';
                    $rarr['result_code'] = 'FAIL';
                    $rarr['result_msg'] = "卡券状态异常";
                }
            } elseif ($userinfo['statu'] == 2) {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = 'ok';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = "该卡券已核销";
            }
        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = 'ok';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = "卡券编号不存在";
        }
//        } catch (\Exception $e) {
//            $rarr['return_code'] = "FAIL";
//            $rarr['return_msg'] = '系统错误';
//            $rarr['result_code'] = 'FAIL';
//            $rarr['result_msg'] = '系统错误' . $e->getMessage();
//        }
        return $rarr;
    }


    public function options($card_id)
    {
        //  Session::set('openid', null);
        $CardMember = new CardMember();
        $CardGeneral = new CardGeneral();
        $Merchant = new Merchant();
//        $System = new System();
        $Agent = new Agent();
        $WxOpen = new WxOpen();
        $mid = $CardMember->where('card_id', $card_id)->value('mid');
        if ($mid) {
            $map['id'] = $mid;
        } else {
            $map['id'] = $CardGeneral->where('card_id', $card_id)->value('mid');
        }
        $merchantinfo = $Merchant->where($map)->field('wx_open_id,agent')->find();
        $mab['id'] = $merchantinfo['wx_open_id'];
//        $config =$System->where(array('id' => 1))->find();
        $users = $WxOpen->where($mab)->find();
        $config = $Agent->where(array('id' => $merchantinfo['agent']))->find();
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }

    public function testtkuser(){
        $map['openid'] = 'oCVCLv2epPfR-zgO8v3HE7y5yjsw';
        $map['pay_fs'] = ['in',[0,2]];
        $map['pay_statu'] = 1;
        $map['add_time'] = ['gt',1540828800];
        $Paylog = new Paylog();
        $Order = new Order();
        $list = $Paylog->where($map)->field('orderid,mid,price')->limit(20,10)->select();
//        var_dump(count($list));
//        exit();
        $res = [];
        foreach ($list as $k=>$v){
            $data['out_trade_no'] = $v['orderid'];
            $data['merchantid'] = 501;
            $data['refund_fee'] = $v['price'];
            $data['device_id'] = '123';
            $data['source'] = '3';
            $data['refund_desc'] = 'pc';
            $res[] = $Order->getorderrefund($data);
        }
        var_dump(count($res));
        var_dump($res);


    }

    public function testjc(){
        $param['out_trade_no'] = 'H1388-20191016102514161029';
        $param['merchantid'] = 1388;
        $Pay = new Pay();
        $res = $Pay->getcheckpay($param);
        var_dump($res);
    }

    public function testgetcheckpay($param)
    {
//        try {
            $map['orderid'] = $param['out_trade_no'];
            $map['mid'] = $param['merchantid'];
            $Paylog = new Paylog();
            $payinfo = $Paylog->where($map)->field('pay_way,pay_fs,pay_cate')->find();
            $pay_way = $payinfo['pay_way'];
            $pay_fs = $payinfo['pay_fs'];
            $pay_cate = $payinfo['pay_cate'];
            if ($pay_cate == "wx_deposit") {
                $DepositPay = new DepositPay();
                $rarr = $DepositPay->wxcheckpay($param);
            } else {
                switch ($pay_way) {
                    case '0':
                        $Officialpay = new OfficialPay();
                        $rarr = $Officialpay->getcheckpay($param, $pay_fs);
                        break;
                    case '1':
                        $rarr = $this->mscheckpay($param);
                        break;
                    case '2':
                        $NotCardPay = new NotCardPay();
                        $rarr = $NotCardPay->getcheckpay($param, $pay_fs);
                        break;
                    case "10":
                        $Lftpay = new Lftpay();
                        $rarr = $Lftpay->checkpay($param);
                        break;
                    case "30":
                        $Lftpay = new Huifupay();
                        $rarr = $Lftpay->checkpay($param);
                        break;
                }
            }
//        } catch (\Exception $e) {
//            $rarr['return_code'] = "FAIL";
//            $rarr['return_msg'] = '系统错误';
//            $rarr['result_code'] = 'FAIL';
//            $rarr['result_msg'] = '系统错误' . $e->getMessage();
//        }
        return $rarr;

    }

    public function getcardusqrcode(){
        $param['add_bonus_mass'] = '1';
        $param['remark_mass'] = '123123123';
        $param['merchantid'] = 501;
        $param['device_id'] = 'zhihuishangquan';
        $CardsThree = new CardsThree();
        $res = $CardsThree->send_user_bonusmsg($param);
        var_dump($res);

    }


    public function getqrcode(){
        $mid = 501;
        $url='https://os.wunengkeji.com';
        $res = $this->qrcode_mach($mid,$url);
        echo "<img src=".$res.">";
//        var_dump($res);
    }


    //门店收款二维码   新版
    public function qrcode_mach($mid = 602, $url = '', $level = 4, $size = 16)
    {
        $map['id'] = $mid;
        $Merchant = new Merchant();
        $urls = $url.'/index.php?s=/Index/pay/id/'.$mid.'.html';


        $qrCode = new QrCode();
        $qrCode->setText($urls)
            ->setSize(570)//大小
            //->setLabelFontPath(VENDOR_PATH . 'endroid/qrcode/assets/noto_sans.otf')
            ->setErrorCorrectionLevel('high')
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0));

        header('Content-Type: ' . $qrCode->getContentType());
        $image = $qrCode->writeString();
//        $imgstr = base64_encode($image);
//        return $imgstr;
        $path = $_SERVER['DOCUMENT_ROOT'] . "/qrcode/pay/" . $mid . '.png';
        $pathimg = "/qrcode/pay/" . $mid . '.png';
        file_put_contents($path, $image);
        return $url.$pathimg;
    }


    public function getczlist(){
        $param['merchantid'] = '501';
        $res = $this->czgive_list($param);
        var_dump($res);
    }





    public function czgive_list($param){
        $Merchant = new Merchant();
        $CardMember = new CardMember();
        $CardGeneral = new CardGeneral();
        $maps['id'] = $param['merchantid'];
        $merchantinfo = $Merchant->where($maps)->field('top_mid,id')->find();
        //判断card_id是不是在该商户下
        $maps2['top_mid'] = $merchantinfo['top_mid'];
//        $maps2['card_id'] = $param['card_id'];
//        $maps2['statu'] = 1;
        $maps2['card_type'] = "MEMBER_CARD";
        $qs = $CardMember->where($maps2)->find();
        if ($qs) {
            $maps1['top_mid'] = $merchantinfo['top_mid'];
            $maps1['statu'] = 1;
//            $maps1['card_id'] = $param['card_id'];
            $CardCzgive = new CardCzgive();
            $list = $CardCzgive->where($maps1)->select();
            if ($list) {
                foreach ($list as $k => $v) {

                    $card_id = $v['zs_card_id_all'];
                    $card_id2 = $v['zs_card_id'];
                    $z1="";
                    $z2="";
                    $resu = '';
                    if ($v['zs_money']) {
                        $z1 = "赠送" . $v['zs_money'] . "元";
                    }

                    if ($v['bonus']) {
                        $z2 = "+" . $v['bonus'] . "积分";
                    }
                    if ($card_id !== '' && $card_id !== null) {
                        $s = json_decode($card_id, true);
                        $ss = "";
                        var_dump($s);
                        foreach ($s as $ks => $vs) {
                            $ss .= $vs['zs_card_id'] . "|" . $vs['zs_no'] . ",";
                        }

                        $card_id_list = explode(',', rtrim($ss, ','));

                        $res = [];
                        foreach ($card_id_list as $ks => $vs) {
                            $ivs = explode('|', $vs);

                            if (filteroffcard($ivs[0]) == true) {

                                $res[$ks]['title'] = $CardGeneral->where('card_id', $ivs[0])->value('title');
                                $res[$ks]['zs_no'] = $ivs[1];
                            }

                        }
                        if (count($res) >= 1) {
                            $resu = '赠卡券:';
                            if (isset($res)) {
                                foreach ($res as $ks => $vs) {
                                    $resu .= $vs['title'] . "(" . $vs['zs_no'] . "张),";
                                }
                                $resu = rtrim($resu, ',');
                            }
                        }
                    } elseif ($card_id2 !== '' && $card_id2 !== null) {
                        $cardlist2 = explode(',', $card_id2);
                        foreach ($cardlist2 as $ks => $vs) {
                            if (filteroffcard($vs) == true) {

                                $res2[$ks] = $CardGeneral->where('card_id', $vs)->value('title');
                            }
                        }
                        if ($res2) {
                            $resu = '+卡券:';
                            $resu .= implode(',', $res2);
                        }
                    }
                    $list[$k]['name'] = "充值" . $v['cz_money'] . "元" . $z1 . $z2 . $resu;
                }
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'SUCCESS';
                $rarr['result_msg'] = '成功';
                $rarr['info'] = $list;
            }else{
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '失败';
            }
        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = '';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '会员卡与商户不匹配';
            $rarr['sql'] = $qs;
            $rarr['sqlqq'] = $CardMember->getLastSql();
        }
        return $rarr;
    }

    //
    public function getuserlist(){
        $param['merchantid'] = 501;
        $param['search_val'] = '';
        $param['code'] = '819240230226';
        $res = $this->carduserinfo($param);
        var_dump($res);
    }


    //获取相关联的商户号列表，
    public function getmidlist($type, $data)
    {

        $Merchant = new Merchant();
        switch ($type) {
            case "mid":
                $top_mid = $Merchant->where('id', $data)->value('top_mid');
                break;
            case "topmid":
                $top_mid = $data;
                break;
        }
        $midlist = $Merchant->where('top_mid', $top_mid)->field('id')->select();
        $mids = [];
        foreach ($midlist as $k => $v) {
            $mids[] = $v['id'];
        }
        return $mids;
    }



    //会员列表
    public function carduserlist($param)
    {
        try {
            $CardUserGet = new CardUserGet();
            $mids = $this->getmidlist('mid', $param['merchantid']);
            $mid = implode(',', $mids);
            $map['a.mid'] = array('in', $mid);
            $map['a.statu'] = 0;
            $map['a.have'] = 1;
            $map['a.fire'] = 1;
            $map['a.card_type'] = 'MEMBER_CARD';
            $map['a.phone|a.code|a.old_code'] = array('like', '%' . $param['search_val'] . '%');
            $page = ($param['page'] - 1) * 10;
//            return $map;
            $cardlist = $CardUserGet->alias('a')
                ->join('wn_card_fans f', 'f.openid=a.openid', 'left')
                ->where($map)
                ->field('a.id,a.card_type,f.head_url,f.nickname,a.code,a.name,a.sex as sex,f.sex as sexs,a.fire,
                f.statu,a.bonus,a.add_bonus,a.use_bonus,a.balance,a.add_balance,a.use_balance,a.phone,a.frequency,a.create_time,a.birthday,a.rank_name')
                ->order('a.id desc')->limit($page, 10)->select();

            if ($cardlist) {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'SUCCESS';
                $rarr['result_msg'] = '';
                $rarr['info'] = $cardlist;
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '获取失败';
                $rarr['info'] = [];
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;

    }

    //获取会员信息
    public function carduserinfo($param)
    {
        try {
            $Merchant = new Merchant();
            $Paylog = new Paylog();
            $top_mid = $Merchant->where('id', $param['merchantid'])->value('top_mid');
            $midlist = $Merchant->where('top_mid', $top_mid)->column('id');
            $mids = $midlist;
            $map['a.mid'] = array('in', $mids);
            $map['a.have'] = 1;
            $map['a.fire'] = 1;
            $map['a.phone|code|old_code'] = $param['code'];
            $map['a.card_type'] = 'MEMBER_CARD';
            $CardUserGet = new CardUserGet();
            $card = $CardUserGet->alias('a')
                ->join('wn_card_fans f', 'f.openid=a.openid', 'left')
                ->field('a.mid,a.id,a.card_type,a.code,a.name,a.sex as sex,a.fire,f.head_url,f.nickname,f.statu,a.remark,
                a.bonus,a.add_bonus,a.use_bonus,a.balance,a.add_balance,a.use_balance,a.phone,a.frequency,a.create_time,a.birthday,a.rank_name,a.openid')
                ->where($map)->find();
            if ($card) {
                $maps['card_type'] = ['neq','MEMBER_CARD'];
                $maps['openid'] = $card['openid'];
                $cardsnum = $CardUserGet->where($maps)->count('id');
                //END
                $maps1['openid'] = $card['openid'];
                $maps1['pay_statu'] = 1;
                $maps1['pay_fs'] = ['in',[0,2]];
                $maps1['pay_cate'] = ['neq','recharge'];
                $pay_fee = $Paylog->where($maps1)->sum('price');
                //END
                $cdata['openid']=$card['openid'];
                $cdata['pay_fee'] = $pay_fee;
                $cdata['cards_num'] = $cardsnum;
                $cdata['name']=$card['name']==''?$card['nickname']:$card['name'];
                $cdata['nickname']=$card['nickname'];
                $cdata['head_url']=$card['head_url'];
                $cdata['phone']=$card['phone'];
                $cdata['rank_name']=$card['rank_name'];
                $cdata['merchantid']=$card['mid'];
                $cdata['balance']=$card['balance'];
                $cdata['bonus']=$card['bonus'];
                $cdata['create_time']=$card['create_time'];
                $cdata['code']=$card['code'];
                $cdata['birthday']=$card['birthday'];
                //END
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'SUCCESS';
                $rarr['result_msg'] = "获取成功";
                $rarr['info'] = $cdata;
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = '';
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = '会员卡不存在';
            }
        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();
        }
        return $rarr;
    }

    public function testqwe(){

        $data['appid'] ='20180501';
        $data['device_id'] ='zhihuishangquan';
        $data['gonghao'] ='1003';
        $data['level'] ='3002';
        $data['merchantid'] ='501';
        $data['method'] ='cashier_add';
        $data['password'] ='123456';
        $data['sex'] ='0';
//        $data['sign'] ='638129f89a4f740baa177c5aa6895efc';                //60d83f4f8c751bcef2ffe1859c9ada78
        $data['signaturenonce'] ='cGnPthkwXQGP4wMiYxaMK5HB5ffWCnNm';
        $data['source'] ='micro_app';
        $data['status'] ='1';
        $data['tel'] ='15836370080';
        $data['timestamp'] ='1570602784';
        $data['username'] ='123456';
        $data['version'] ='2.0';
        $data['xingming'] ='123';
//        $data['key'] ='83d96c2aec5deaa8e2d675d9121cef4e1';
//        var_dump(json_encode($data,JSON_UNESCAPED_UNICODE));
//        exit();
//        $param1 = json_decode($data, true);
        $param1 = $data;
        $param['appid'] = '20180501';
//        $res = uni_array($param1);
//        var_dump($res);
//        exit();
        $res = $this->getsign(uni_array($param1), "MD5", $param['appid']);
        var_dump($res);
    }


    public function getsign($arrdata, $method = "sha1", $appid = "")
    {
        try {
            if (!function_exists($method)) return false;
            ksort($arrdata);
            $paramstring = "";
            foreach ($arrdata as $key => $value) {
                if (isset($value) && !empty($value)) {
                    if (is_array($value)) {
                        $value = json_encode($value, JSON_UNESCAPED_UNICODE);
                    }
                    $str[] = $key . "=" . $value;
                }
            }
            $paramstring = join("&", $str);
            if (!empty($appid)) {
                $signkey = getsignkey($appid);
            } else {
                $signkey = getsignkey($arrdata['appid']);
            }
            $Sign = $paramstring . "&key=" . $signkey;
//            $Sign = $method($paramstring . "&key=" . $signkey);

            return $Sign;
        } catch (\Exception $e) {
            return $e;
        }
    }


    public function testrec(){
        $arr = ' {
	"pay_fs": "0",
	"out_trade_no": "H1333-20190907162905819977",
	"device_id": "wuye",
	"sysid": "1",
	"method": "cardbalancerec",
	"card_code": "60020000000121",
	"csid": "",
	"merchantid": "1333",
	"num": "0.01",
	"pay_cate": "Cash",
	"source": "web",
	"action_user": "嗨呀"
}';
        $data = json_decode($arr,true);
        $Cards = new Cards();
        $res=  $Cards->cardbalancerec($data);
        var_dump($res);
    }

    public function testordercx(){
//        $arr = ' {
//			"appid": "20180501",
//			"method": "orderquery",
//			"merchantid": "1328",
//			"out_trade_no": "H1328-20190907091805170301",
//			"source": "online",
//			"version": "2.0",
//			"timestamp": 1567819093,
//			"signaturenonce": 7917945,
//			"sign": "a238de0750c8fb745a182da9bec0cfeb"
//		}';

        $arr = '{
			"appid": "20180501",
			"method": "orderquery",
			"merchantid": "1328",
			"out_trade_no": "H1328-20190907091708497155",
			"source": "online",
			"version": "2.0",
			"timestamp": 1567819035,
			"signaturenonce": 9365198,
			"sign": "59c7e670acf818e5cf102b0773b44f17"
		}';
        $data = json_decode($arr,true);
        $Pay = new Pay();
        $res=  $Pay->getcheckpay($data);
        var_dump($res);
    }

    public function testrecfee(){
        $arr = '{"out_trade_no":"R-201909021505192691","device_id":"wuye","sysid":"1","method":"cardbalancerec","card_code":"609414623905","csid":"","merchantid":"1328","num":0.01,"pay_cate":"Cash","source":"web","action_user":"王晓晨","appid":"20180501","signaturenonce":22064057,"timestamp":1567407926,"version":"2.0","sign":"d336e62f7173fc6764f4e09a55f4bf5d"}';
        $data = json_decode($arr,true);
        $Cards = new Cards();
        $res = $Cards->cardbalancerec($data);
        var_dump($res);
    }

    public function index()
    {
        die('非法访问');
    }
    public function testa2b(){
        $User = new Merchant();
        $Agent = new Agent();
        $users = $User->where(array('id' => 501))->find();
        $agent = $Agent->where(array('id' => $users['agent']))->find();
        $options = array(
            'appid' => $agent['ali_app_id'], // 填写高级调用功能的app id
            'rsaPrivateKey' => $agent['ali_app_key'],
        );
        $aliObj = new Alipayobj($options);
        $price = "1.00";
        $sh = 501;
        $out_trade_no = "test-".date('YmdHis').rand(1000,9999);
        $body = "快捷支付";

        $auth_code = "285744973329195967";


        $total_fee = $price;
        $data['app_id'] = $agent['ali_app_id'];
        $data['method'] = "alipay.fund.auth.order.freeze";
        $data['charset'] = "UTF-8";
        $data['sign_type'] = "RSA2";
        $data['timestamp'] = date('Y-m-d H:i:s');
        $data['version'] = "1.0";
        $data['notify_url'] = 'http://api.wunengkeji.com';
        $data['app_auth_token'] = $users['ali_merid'];
        $data['biz_content'] = json_encode(array(
            'auth_code' => $auth_code,
            'auth_code_type' => "bar_code",
            'out_order_no' => $out_trade_no,
            'out_request_no' => date('YmdHis')."111",
            'payee_user_id' => "2088431658842010",
//            'scene' => 'bar_code',
            'order_title' => '123456',
            'amount' => $total_fee,
            'product_code' => "PRE_AUTH",
//            'subject' => $body,
//            'alipay_store_id' => $users['ali_store_id'],
//                'store_id'=>I('get.id'),
//            'extend_params' => array(
//                'sys_service_provider_id' => $agent['sys_service_provider_id'],
//            ),
        ));
        $signContent = $aliObj->rsaSign($data, $signType = "RSA2");

        $data['sign'] = $signContent;
        var_dump($data);
//        exit();
        $sj = $aliObj->scanpay($data);
        var_dump($sj);
        exit();
        $content = json_decode(iconv("GB2312", "UTF-8//IGNORE", $sj), true);
        $rarr['return_code'] = "FAIL";
        $rarr['return_msg'] = '系统错误';
        $rarr['result_code'] = 'FAIL';
        $rarr['result_msg'] = json_encode($sj,true);

        return $rarr;
    }

    public function testsend(){
        $merchantid = '1351';
        $bus_orderid = "bus1351-20190812090855603655";
        $notify_param['merchantid'] = $merchantid;
        $notify_param['bus_orderid'] = $bus_orderid;
        $CreateNotify = new CreateNotify();
        $res = $CreateNotify->bonus_con_notify($notify_param);
        var_dump($res);
    }

    public function qweqwe(){
        $CreateNotify = new CreateNotify();
        $notify_param['merchantid'] = '1329';
        $notify_param['bus_orderid'] = 'bus1339-20190705192955939001';
        $res = $CreateNotify->balance_rec_notify($notify_param['bus_orderid']);
        var_dump(json_encode($res,true));
    }

    public function sendzm(){

        $busid = "bus1339-20190705192955939001";
        $type = "balance_rec";
        $ChildCardModify = new ChildCardModify();
        $res = $ChildCardModify->createmodify($type,$busid);
        var_dump($res);
    }

    public function testsends()
    {
        $Notify = new CreateNotify();
//        $param['out_trade_no']="H501-20190620113156464966";
//        $param['merchantid']=501;
//        $Notify->pay_notify($param);
//        $s = "bus501-20190620160258198406";
//        $ss=$Notify->balance_con_notify($s);
//        var_dump($ss);
//        $Pub=new Pub();
//        $rarr = $Pub->getorderinfo("H501-20190620113132198992");
//        $rarr['record_id'] = 1;
//        $rarr['bus_orderid'] = "bus501-20190620113132689541";
//        $ss=$Notify->balance_con_notify($rarr);
        $param['merchantid']="501";
        $param['code']="316566081521";
        $ss=$Notify->quancon_notify($param);
        var_dump($ss);

    }

    public function syncnotify()
    {
        $SendNotify = new SendNotify();
        $data = array('a' => 132131232);
        $s = $SendNotify->task_biz_notify($data);
        var_dump($s);


    }

    public function test2()
    {
        $Deviceobj = new Deviceobj();
        $s = $Deviceobj->ylb_sendvoice("H501-20190321151122451666");
        var_dump($s);
    }

    public function wxorderquery($id = "1059", $orderid = "H1059-20190219193839575199")
    {

        try {
            $map['id'] = $id;
            $config = Db::name('Merchant')->where($map)->find();
            $agent = Db::name('agent')->where(array('id' => $config['agent']))->find();
            $users = Db::name('wx_open')->where(array('id' => $agent['wx_open_id']))->find();
            $userwx = Db::name('wx_open')->where(array('id' => $config['wx_open_id']))->find();

            $options = array(
                'appid' => $users['authorizer_appid'], // 填写高级调用功能的app id
                'partnerid' => $agent['partnerid'], // 财付通商户身份标识
                'partnerkey' => $agent['partnerkey'], // 财付通商户权限密钥Key
            );
            $weObj = new Wxobjopen($options);
            $appid = $users['authorizer_appid'];
            $mch_id = $agent['partnerid'];
            $out_trade_no = $orderid;
            $sub_mch_id = $config['sub_mch_id'];
            $sub_appid = $userwx['authorizer_appid'];
            $nonce_str = $weObj->generateNonceStr();
            $arrdata = array(
                "appid" => $appid,
                "mch_id" => $mch_id,
                "sub_appid" => $sub_appid,
                "sub_mch_id" => $sub_mch_id,
                "nonce_str" => $nonce_str,
                "out_trade_no" => $out_trade_no,
                "sign_type" => "HMAC-SHA256"
            );
            $orderxml = $weObj->createWxPackageXml($arrdata);
//            $orderxml = $weObj->createOrderXml($appid, $mch_id, $sub_appid, $sub_mch_id, $out_trade_no, $nonce_str);
            $url = "https://api.mch.weixin.qq.com/deposit/orderquery";
            $rarr = $weObj->send_post($url, $orderxml);

        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = '系统错误';
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = '系统错误' . $e->getMessage();;
        }
        var_dump($rarr);

    }

    public function testff()
    {
        $str = '{"mid":"979","orderid":"H979-20190107112057839079_YJ","ms_orderid":null,"pay_statu":1,"pay_cate":"wx_deposit","openid":"ofz7LwmAJpiXRaue87cTHMCeZx74","sub_openid":"oUwXAwfqMEz48tfopXKLxo59gMOY","ser_openid":null,"price":0,"yf_price":"1.00","zk_price":"0.00","service_charge":"0.00","orderno":"4200000225201901076026723896","pay_time":"2019-01-07 11:20:58","add_time":1546832022,"pay_way":"0","tzzt":"1","cashier":null,"payinfo":"{\"return_code\":\"SUCCESS\",\"appid\":\"wx24d738fdd659f2ee\",\"mch_id\":\"1302428301\",\"sub_mch_id\":\"1372279502\",\"nonce_str\":\"8YA6dAX9zXxTJYnz\",\"sign\":\"1FFEA904AF9B51A9C2BC428DB16C293366A421220A1A7E21574FB27EFD5509D7\",\"result_code\":\"SUCCESS\",\"transaction_id\":\"4200000225201901076026723896\",\"out_trade_no\":\"H979-20190107112057839079\",\"total_fee\":\"100\",\"consume_fee\":\"100\",\"fee_type\":\"CNY\"}","g_zt":"0","pay_fs":2,"tzmsg":"[]","kuantai":"lszmac","d_cashier":"\u6735\u513f\u5168","tk_status":"0","tk_price":"0.00","cash_fee":"0.00","coupon_fee":"0.00","dis_card_zk":"0.00","vip_card_zk":"0.00","gel_card_zk":"0.00","hx_card":null,"hx_card_res":null,"hx_card_statu":"0","cz_card_id":null,"get_card_id":"0","pay_is_put":"0","card_num":null,"wsy_orderid":null,"del_statu":"0","trade_no":null,"huifu_ordid":null,"source":"pc","client_source":"1","is_efp_receipt":"N","attach":null}';
        $Paylog = new Paylog();


        var_dump(json_decode($str));
    }

    public function xf()
    {

        try {
            $param['out_trade_no'] = "H979-20181230165141870172";
            $map['id'] = 979;
            $User = new Merchant();
            $Agent = new Agent();
            $WxOpen = new WxOpen();
            $config = $User->where($map)->find();
            $Paylog = new Paylog();
            $payinfo = $Paylog->where('orderid', $param['out_trade_no'])->find();

            $agent = $Agent->where(array('id' => $config['agent']))->find();
            $users = $WxOpen->where(array('id' => $agent['wx_open_id']))->find();
            $options['wx_open_id'] = $agent['wx_open_id'];
            $weObj = new Wxtk($options);
            $notice = $weObj->generateNonceStr();
            $weObj->setParameter('appid', $users['authorizer_appid']);
            $weObj->setParameter('mch_id', $agent['partnerid']);
            $weObj->setParameter('sub_mch_id', $config['sub_mch_id']);
            $weObj->setParameter('total_fee', $payinfo['price'] * 100);
            $weObj->setParameter('consume_fee', 1);
            $weObj->setParameter('nonce_str', $notice);
            $weObj->setParameter('fee_type', "CNY");
            $weObj->setParameter('out_trade_no', $param['out_trade_no']);
            $weObj->setParameter('sign_type', "HMAC-SHA256");
            $sss = $weObj->create_hash_xml($agent['partnerkey']);
            $url = 'https://api.mch.weixin.qq.com/deposit/consume';
            $responseXml = $weObj->curl_post_ssl($url, $sss);
            $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $responseArr = ( array )$responseObj;
            var_dump($responseArr);
            if ($responseArr['return_code'] == "SUCCESS" && $responseArr['result_code'] = "SUCCESS") {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = "";
                $rarr['result_code'] = 'SUCCESS';
                $rarr['result_msg'] = "订单消费成功";
                $rarr['out_trade_no'] = $param['out_trade_no'];
//                $sdata['pay_statu'] = 8;
//                $Paylog = new Paylog();
//                $Paylog->save($sdata, ['orderid' => $param['out_trade_no']]);

            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['return_msg'] = "";
                $rarr['result_code'] = 'FAIL';
                $rarr['result_msg'] = "订单消费失败";
            }

        } catch (\Exception $e) {
            $rarr['return_code'] = "FAIL";
            $rarr['return_msg'] = "系统错误" . $e->getMessage();
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = "系统错误" . $e->getMessage();
        }
        var_dump($rarr);

    }

    public function t1()
    {
        $array = array('id' => 1);
        $data = array('id' => 2);
        $logs['type'] = "apilog";
        $logs['up'] = $array;
        $logs['down'] = $data;
        $s = $this->recdata($logs);
        var_dump($s);

    }

    public function recdata($arr)
    {

        try {
            file_put_contents('/data2/html/osapi/clientlog/' . date("Y-m-d") . '.txt', $arr['type'] . '-' . json_encode($arr, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
        } catch (\Exception $e) {
            $data['return_code'] = "SUCCESS";
            $data['result_code'] = "FAIL";
            $data['result_msg'] = "系统错误";
            $data['return_msg'] = "系统错误" . $e->getMessage();

        }

    }

    public function testsss()
    {
        $a = 0.9;
        $b = 0.72;

        $c = $this->format_price3($a * 100 - $b * 100);
        echo $a;
        echo "<br />";
        echo $b;
        echo "<br />";
        echo $c;
        echo "<br />";


    }

    public function test6()
    {
        $type = "pay_user";
        $data['merchantid'] = "501";
        $data['orderid'] = "H501-2018070320002566584";
        $TemplateMsg = new TemplateMsg();
        $res = $TemplateMsg->send_tpl($type, $data);
        var_dump($res);

    }

    public function test5()
    {

        $Bonus = new Bonus();
        $arr['price'] = 2;
        $arr['merchantid'] = 501;
        $arr['username'] = "test001";


        $s = $Bonus->pay_bonus("oCVCLv2epPfR-zgO8v3HE7y5yjsw", $arr);
        var_dump($s);

    }

    public function tests3()
    {

        $ss = $this->checkappid('20180508');
        var_dump($ss);
    }

    private function checkappid($appid, $merchantid = "")
    {
        $map['sign_appid'] = $appid;
        $Agent = new Agent();
        $Agents = $Agent->where($map)->field('id')->select();

        foreach ($Agents as $k => $v) {
            $agentid[] = $v['id'];
        }
        $sid = implode(',', $agentid);
        $mab['agent'] = array('in', $sid);

        $Merchant = new Merchant();
        $mach = $Merchant->where($mab)->find();

        if ($mach) {
            return "SUCCESS";
        } else {
            return "FAIL";
        }


    }

    public function tests2()
    {


        echo bcadd(1.001, 1.002, 2);
    }

    public function format_price3($param)
    {
        $price = (float)$param;//将数值转换为浮点数
        $str = explode(".", $price);//将字符串转换为数组
        if (isset($str[1])) {//判断是否存在小数点
            $str2 = strlen($str[1]);//获取小数点位数
            if ($str2 <= 2) {//当小数点位数小于等于2 原样输出
                $data = $price;
            } else {
                $str4 = substr($str[1], 0, 2);//取出小数点后两位的值
                $str5 = substr($str[1], 0, 1);//取出小数点后1位的值
                if ($str4 == "00") {//当小数点后2为未00时直接+1
                    $str3 = "01";
                } elseif ($str5 == "0") {//当小数点后1为为0时直接最后1位+1
                    if (substr($str[1], 1, 1) + 1 !== 10) {
                        $str3 = (string)$str5 . (substr($str[1], 1, 1) + 1);
                    } else {
                        $str3 = substr($str[1], 0, 1) + 1;
                    }
                } else {//不满足上述条件直接+1
                    $str3 = substr($str[1], 0, 2) + 1;
                }
                if ($str3 == 100) {//当金额为X.99X直接整数+1
                    $data = ($str[0] + 1);
                } else {
                    $data = $str[0] . '.' . $str3;
                }
            }
        } else {
            $data = $price;
        }
        return $data;
    }

    public function canshu()
    {

        $data['search_vals'] = "";
        $data['page'] = "1";
        return $data;
    }

    public function testsign()
    {
        $aa = MD5("appid=20180501&device_code=be79636283a81de03ae000cf0c724080&device_id=lsz001&merchantid=501&method=orderrefund&out_trade_no=H501-20180606190227536558&password=123&refund_desc=0&refund_fee=0.01&signaturenonce=df263d996281d984952c07998dc54358&source=1&timestamp=1528530876&version=2.0&key=83d96c2aec5deaa8e2d675d9121cef4e1");
        echo $aa;
    }

    public function tests()
    {
        $str = 20180606160605;
        $str2 = date('Y-m-d H:i:s', strtotime($str));

        echo $str2;
    }

    public function test()
    {
        //参数
        $data = $this->canshu();
        $data['method'] = "cardlist";
        $data['appid'] = "20180501";
        $data['merchantid'] = "501";
        $data['version'] = "V2.0.0";
        $data['timestamp'] = time();
        $data['signaturenonce'] = "123456";

        $data['sign'] = getsign(uni_array($data), "MD5");
        echo json_encode($data);
        exit();
        $s = $this->http_post('http://api.wunengkeji.com/api.php', json_encode($data));

        var_dump($s);
        exit();
    }

    private function http_post($url, $param, $post_file = false)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
            $is_curlFile = true;
        } else {
            $is_curlFile = false;
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
            }
        }
        if (is_string($param)) {
            $strPOST = $param;
        } elseif ($post_file) {

            if ($is_curlFile) {
                foreach ($param as $key => $val) {

                    if (substr($val, 0, 1) == '@') {

                        $param[$key] = new \CURLFile(realpath(substr($val, 1)));

                    }
                }

            }
            $strPOST = $param;

        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);

        }

        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type:application/json; charset=utf-8',

            'Content-Length: ' . strlen($param)));
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);

        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }
}
